﻿using System;
using System.Web;
using Portal.Core;
using Portal.Data.Entity;
using Portal.Data.Security;

namespace Portal.Data
{
    public class PortalContext : CoreContext
    {
        public static PortalUser CurrentUser
        {
            get
            {
                PortalUser pu = null;
                try
                {
                    //first check from session
                    if (HttpContext.Current.Session[SecurityProvider.UserCookieName] != null)
                    {
                        return (PortalUser)HttpContext.Current.Session[SecurityProvider.UserCookieName];
                    }

                    var aCookie = HttpContext.Current.Request.Cookies[SecurityProvider.UserCookieName];
                    if (aCookie != null && aCookie.Value != null)
                    {
                        //out session but keep current cookie, set for session again
                        var idPassSplit = aCookie.Value.Split('-');
                        pu = PortalUser.FindByIdAndPasswordHash(Convert.ToInt32(idPassSplit[0]), idPassSplit[1].Trim());
                        if (pu != null)
                            SecurityProvider.Instance.SetUserCookie(pu);
                    }
                }
                catch
                {
                }
                return pu;
            }
        }

        #region Caching

        public const string PrefixWeb = "web";
        public const string PrefixMobi = "mobi";
        #endregion

        public static string TitleExtension(HttpRequest req)
        {
            var exts = "";
            int page;
            int.TryParse(req["page"], out page);
            var ch = req["char"] ?? "";
            if (page > 1)
            {
                exts += @" | Trang " + page;
            }
            if (ch.Trim().Length > 0)
            {
                exts += @" | Ký tự " + ch;
            }
            return exts;
        }

        /// <summary>
        /// 0:ID, 1:FullName, 2:Totalchap, 3:visit, 4:status
        /// </summary>
        public const string BookByAccountDisplayFormat = "{1} - [Mã: {0}, số chương:{2}, xem:{3}, trạng thái:{4}]";

//        #region Set/Get RealUrl
//        /// <summary>
//        /// Set the real url at current view
//        /// </summary>
//        /// <param name="real"></param>
//        public static void SetRealUrl(string real)
//        {
//            HttpContext.Current.Session["poratl@realurl"] = Common.GetAbsoluteUrl(real);
//        }
//        /// <summary>
//        /// Get and destroy old value
//        /// </summary>
//        /// <returns></returns>
//        public static string GetRealUrl()
//        {
//            var realUrl = HttpContext.Current.Session["poratl@realurl"] ?? "";
//            HttpContext.Current.Session["poratl@realurl"] = null;
//            return realUrl.ToString();
//        }
//        #endregion
    }
}